home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / gfx / show / gs_src_amiga.lha / ami_open.c next >
C/C++ Source or Header  |  1996-03-11  |  3KB  |  142 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <limits.h>
  6. #include <errno.h>
  7. #undef LONGBITS
  8. #undef BITSPERBYTE
  9. #undef MAXINT
  10. #undef MININT
  11. #include <dos/dos.h>
  12. #define DEVICES_TIMER_H
  13. #include <dos/dosextens.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <stabs.h>
  17.  
  18. extern void __seterrno(void);
  19. extern char *__amigapath(const char *path);
  20. extern struct WBStartup *_WBenchMsg;
  21.  
  22. unsigned long *__stdfiledes;
  23. static unsigned long stdfilesize=3;
  24. static long stderrdes=0; /* The normal Amiga shell sets no process->pr_CES stream -
  25.                           * we use Open("*",MODE_NEWFILE) in this case
  26.                           */
  27.  
  28. void __initstdio(void)
  29. { unsigned long *sfd;
  30.  
  31.   if((sfd=__stdfiledes=(unsigned long *)malloc(3*sizeof(unsigned long)))==NULL)
  32.     exit(20);
  33.   sfd[STDIN_FILENO ]=Input();
  34.   sfd[STDOUT_FILENO]=Output();
  35.   if(!(sfd[STDERR_FILENO]=((struct Process *)FindTask(NULL))->pr_CES))
  36.     if(_WBenchMsg!=NULL||!(sfd[STDERR_FILENO]=stderrdes=Open((unsigned char *)"*",MODE_OLDFILE)))
  37.       sfd[STDERR_FILENO]=sfd[STDOUT_FILENO];
  38. }
  39.  
  40. /* Call our private constructor */
  41. ADD2INIT(__initstdio,-30);
  42.  
  43. void __exitstdio(void)
  44. { long file,*sfd;
  45.   int i,max;
  46.  
  47.   for(sfd=(long *)&__stdfiledes[3],max=stdfilesize,i=3;i<max;i++)
  48.     if((file=*sfd++))
  49.       Close(file);
  50.  
  51.   if((file=stderrdes))
  52.     Close(file);
  53. }
  54.  
  55. /* Call our private destructor at cleanup */
  56. ADD2EXIT(__exitstdio,-30);
  57.  
  58. int open(const char *path,int flags,...)
  59. {
  60.   unsigned long *sfd;
  61.   int file,max;
  62.  
  63. #ifdef IXPATHS
  64.   if((path=__amigapath(path))==NULL)
  65.     return -1;
  66. #endif
  67.  
  68.   for(sfd=__stdfiledes,max=stdfilesize,file=0;file<max;file++)
  69.     if(!sfd[file])
  70.       break;
  71.  
  72.   if(file>SHRT_MAX)
  73.   { errno=EMFILE;
  74.     return -1; }
  75.  
  76.   if(file==max)
  77.   { if((sfd=realloc(sfd,(file+1)*sizeof(unsigned long)))==NULL)
  78.     { errno=ENOMEM;
  79.       return -1; }
  80.     __stdfiledes=sfd;
  81.     stdfilesize++;
  82.   }
  83.  
  84.   if (flags&O_TRUNC) {
  85.     DeleteFile((unsigned char *)path);
  86.   }
  87.   if((sfd[file]=Open((unsigned char *)path,
  88.                    flags&(O_WRONLY|O_RDWR)?MODE_READWRITE:MODE_OLDFILE)))
  89.     return file;
  90.   __seterrno();
  91.   return EOF;
  92. }
  93.  
  94. int close(int d)
  95. {
  96.   int ret=0;
  97.   if(d>2)
  98.   {
  99.     if(!Close(__stdfiledes[d]))
  100.     { ret=EOF;
  101.       __seterrno(); }
  102.     __stdfiledes[d]=0;
  103.   }
  104.   return ret;
  105. }
  106.  
  107. off_t lseek(int d,off_t offset,int whence)
  108. {
  109.   long r,file=__stdfiledes[d];
  110.   __chkabort();
  111.   r=Seek(file,offset,whence==SEEK_SET?OFFSET_BEGINNING:
  112.          whence==SEEK_END?OFFSET_END:OFFSET_CURRENT);
  113.   if(r!=EOF)
  114.     r=Seek(file,0,OFFSET_CURRENT);
  115.   if(r==EOF)
  116.     __seterrno();
  117.   return r;
  118. }
  119.  
  120. ssize_t read(int d,void *buf,size_t nbytes)
  121. {
  122.   long r;
  123.   __chkabort();
  124.   r=Read(__stdfiledes[d],buf,nbytes);
  125.   if(r==EOF)
  126.     __seterrno();
  127.   return r;
  128. }
  129.  
  130. ssize_t write(int d,const void *buf,size_t nbytes)
  131. {
  132.   long r;
  133.   __chkabort();
  134.   r=Write(__stdfiledes[d],(char *)buf,nbytes);
  135.   if(r==EOF)
  136.     __seterrno();
  137.   return r;
  138. }
  139.  
  140. int isatty(int d)
  141. { return IsInteractive(__stdfiledes[d]); }
  142.